package com.zdxk.msgPush.websocket.controller;

import com.zdxk.msgPush.msg.observer.StockObserver;
import com.zdxk.msgPush.msg.observer.interfaces.IObserver;
import com.zdxk.msgPush.msg.subject.StockSubject;
import com.zdxk.msgPush.websocket.annotion.WebsocketController;
import com.zdxk.msgPush.websocket.annotion.WebsocketRequestMapping;
import com.zdxk.msgPush.websocket.websocket.ImportDictValueSocket;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;


@WebsocketController
@WebsocketRequestMapping("websocketService")
public class WebsocketServiceController {

    @Autowired
    private ImportDictValueSocket importDictValueSocket;

    @WebsocketRequestMapping("testWebsocket")
    public String testWebsocket(String msg) throws IOException {
        importDictValueSocket.sendInfo(msg);
        return "ok";
    }

    @WebsocketRequestMapping("orderStock")
    public String orderStock(String tscode, String channelId) throws IOException {
        IObserver observer = StockSubject.getInstance().getObserverMap().get(channelId);
        if(observer == null){
            observer = new StockObserver(channelId);
            StockSubject.getInstance().getObserverMap().put(channelId,observer);
        }
        ((StockObserver)observer).addTsCode(tscode);
        return "ok";
    }
}
